import socket
from concurrent.futures import ThreadPoolExecutor


class TCPServer:
    def __init__(self):
        self.sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        print("绑定的服务器为：127.0.0.1:7799")
        self.sock.bind(("127.0.0.1",7799))
        self.sock.listen(100)

    def handle_request(self,cil_sock,addr):
        content=cil_sock.recv(1024)
        print("客户发来的数据为：",content.decode())
        print("客户端发送过来的数据为:", content.decode())
        header = 'HTTP/1.1 200 OK\r\n'
        header += 'Content-Type: text/html;charset=utf-8\r\n'
        header += '\r\n\r\n'
        with open("denglu.html",'rb') as f:
            body=f.read()
        data=header.encode()+body
        cil_sock.send(data)
        cil_sock.close()


    def run(self):
        with ThreadPoolExecutor(max_workers=20) as pool:
            while True:
                cil_sock,addr=self.sock.accept()
                pool.submit(self.handle_request,cil_sock,addr)

if __name__ == '__main__':
    TCPServer().run()
